package net.actions.sys;

import java.util.Map;
import java.util.List;
import java.util.HashMap;
import java.util.Iterator;

import net.base.BaseAction;
import net.base.BaseDao;

import org.apache.log4j.Logger;

public class ModuleAction extends BaseAction {
    private BaseDao dao = null;

    public BaseDao getDao() {
        return dao;
    }

    public void setDao(BaseDao dao) {
        this.dao = dao;
    }

    private Logger log = Logger.getLogger(this.getClass());

    public String insertModule() throws Exception{
        try{
            Map map = this.parserRequest(this.getRequest());
            log.info("执行sql+++++++++++++++++++" + map.get("SQLID"));
            Object i = dao.insert((String) map.get("SQLID"), map);

          //保存 超级管理员的权限
            Map paramMap = new HashMap();
            paramMap.put("ROLE_DM","1");
            paramMap.put("MODULE_DM",i);
           dao.insert("role.insertRoleModule",paramMap);
        }catch (Exception e){
            e.printStackTrace();
            log.error(e.toString());
            code = -1;
            msg = "模块添加失败！\n" + e.getMessage();
            expMsg = getExceptionMessage(e);
            throw new Exception();
        }
        return getResult();
    }

    public void deleteModule() throws Exception{
        try {
            Map map = this.parserRequest(this.getRequest());
            String[] module_dm = String.valueOf(map.get("IDS")).split(",");

            // 循环删除模块
            for (int n = 0; n < module_dm.length; n++) {
                // 先删除模块
                deleteModuleBySJ(module_dm[n]);

                //删除角色模块的对应关系
                dao.delete("role.deleteRoleModuleByModuleDM", module_dm[n]);

                //删除模块
                dao.delete("module.deleteModule", module_dm[n]);
            }
            this.setResult("00", "删除成功！");
        } catch (Exception e) {
            log.error(e.toString());
            this.setResult("99", "删除失败！\n" + e.toString());
            throw new Exception();
        }
    }

    private void deleteModuleBySJ(String module_dm_sj) throws Exception {
        Map parMap = new HashMap();
        parMap.put("MODULE_DM_SJ", module_dm_sj);
        List list = dao.getList("module.selectModuleAll", parMap);
        Iterator it = list.iterator();
        while (it.hasNext()) {
            Map m = (Map) it.next();
            deleteModuleBySJ(String.valueOf(m.get("MODULE_DM")));
        }
        dao.delete("module.deleteModuleByMODULE_DM_SJ", module_dm_sj);
        //删除角色模块的对应关系
        dao.delete("role.deleteRoleModuleByModuleDM", module_dm_sj);
    }

    /**
     * 取模块树
     */
    public void getModuleTree() {
        try {
            StringBuffer xml = new StringBuffer();
            Map map = this.parserRequest(this.getRequest());
            xml.append("<?xml version=\"1.0\" encoding=\"GBK\"?>");
            xml.append("<tree id=\"0\">");
            xml.append(getTreeString( map));
            xml.append("</tree>");
            this.setResult("00", xml.toString());
        } catch (Exception e) {
            log.error(e.toString());
            this.setResult("99", "操作失败！\n" + e.toString());
        }
    }

    /**
     * 取模块树xml
     *
     * @param map
     * @return
     * @throws Exception
     */
    private String getTreeString(Map map) throws Exception {
        StringBuffer xml = new StringBuffer();
        List list = dao.getList("module.selectModuleAll", map);
        Iterator it = list.iterator();
        while (it.hasNext()) {
            Map m = (Map) it.next();

            xml.append("<item text=\"" + m.get("MODULE_NAME")).append("\"").
                    append(" id=\"").append(m.get("MODULE_DM")).append("-").append(m.get("ORDERSALL")).append("\" checked=\"").append("")
                    .append("\" im0=\"tombs.gif\" im1=\"folderOpen.gif\" im2=\"iconSafe.gif\">");
            map.put("MODULE_DM_SJ",m.get("MODULE_DM"));
            xml.append(getTreeString(map));
            xml.append("</item>");
        }
        return xml.toString();
    }
}
